package com.nf.health.app.sweat.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.activity.my.MyShopStoreActivity;
import com.nf.health.app.adapter.OnMyClickListener;
import com.nf.health.app.customview.SweatSetPopupWindow;
import com.nf.health.app.http.Act;
import com.nf.health.app.sweat.adapter.SweatViewPagerAdapter;
import com.nf.health.app.sweat.fragment.SweatTwoFragment;
import com.nf.health.app.sweat.fragment.SweatOneFragment;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.ViewUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by he_pan on 2016/5/21.
 */
public class SweatAnalyzerNewActivity extends BaseActivity {

    @Bind(R.id.titlebar_back_view)
    ImageView titlebarBackView;
    @Bind(R.id.titlebar_title_tv)
    TextView titlebarTitleTv;
    @Bind(R.id.titlebar_right_view)
    ImageView titlebarRightView;
    @Bind(R.id.titlebar_right_tv)
    TextView titlebarRightTv;
    @Bind(R.id.tl_indicator)
    TabLayout tlIndicator;
    @Bind(R.id.vp_content)
    ViewPager vpContent;

    private int collection_frequency = 10;//采集频率
    private SweatOneFragment mUpFragment;
    private SweatTwoFragment mDownFragment;
    private boolean isSetDownFreq = false;//是否是设置下路频率
    private List<String> mMeasurePosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_sweat_analyzer_new);//汗液数据 layout布局
        ButterKnife.bind(this);
        initView();
        initListener();
        initData();
    }

    private void initData() {
        provider.getUserSweatSetting();
    }

    private void initView() {
        ViewUtil.setText(this, R.id.titlebar_title_tv, "汗液数据");
        titlebarRightView.setImageResource(R.drawable.ic_title_setting);
        SweatViewPagerAdapter viewPagerAdapter = new SweatViewPagerAdapter(getSupportFragmentManager());
        mUpFragment = new SweatOneFragment();
        mDownFragment = new SweatTwoFragment();
        viewPagerAdapter.addFragment(mUpFragment, "设备(1)");
        viewPagerAdapter.addFragment(mDownFragment, "设备(2)");
        vpContent.setAdapter(viewPagerAdapter);//设置适配器
        tlIndicator.setupWithViewPager(vpContent);
    }


    private void initListener() {
        titlebarRightView.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.titlebar_right_view://右上角弹出popupwindow
                showPop();
                break;
        }
    }

    private void showPop() {
        SweatSetPopupWindow pop = new SweatSetPopupWindow(this);
        pop.showAsDropDown(titlebarRightView);
        pop.setOnMenuItemClickListener(new OnMyClickListener() {
            @Override
            public void onClick(int position, String flag) {
                if (R.id.tv_set_freq_up == position) {
                    //TODO 采样频率
                    showSampleFrequency();
                    isSetDownFreq = false;
                } else if (R.id.tv_set_freq_down == position) {
                    isSetDownFreq = true;
                    showSampleFrequency();
                } else if (R.id.tv_have_no_device == position) {
                    ActivityUtils.showActivity(SweatAnalyzerNewActivity.this, MyShopStoreActivity.class);
                }
            }
        });
    }

    /**
     * 显示采样频率
     */
    private void showSampleFrequency() {
        View customView = View.inflate(this, R.layout.sweat_freq_edittext, null);
        final EditText et = (EditText) customView.findViewById(R.id.edit_text);
        et.setText(collection_frequency + "");
        et.setSelection(et.getText().length());
        int color = 0xbb2E86F4;
        new MaterialDialog.Builder(this)
                .titleColor(color)
                .positiveColor(color)
                .negativeColor(color)
                .title("设置采集频率:")
                .positiveText("设置")
                .negativeText("退出")
                .customView(customView, true)
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                        LogUtil.e("hepan频率", et.getText().toString());
                        String str = et.getText().toString().trim();
                        if (TextUtils.isEmpty(str)) {
                            return;
                        }
                        int freq = Integer.valueOf(str);
                        if (freq <= 0 || freq > 300) {
                            showToast("请输入正确范围1-300");
                        }
                        if (!isSetDownFreq) {
                            mUpFragment.sendCommandToSweat(freq);
                        } else {
                            mDownFragment.sendCommandToSweat(freq);
                        }
                    }
                }).onNegative(new MaterialDialog.SingleButtonCallback() {
            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                dialog.dismiss();
            }
        }).show();
    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        if (Act.Flag.GET_SWEAT_SETTING_FLAG.equals(actionName)) {
            mMeasurePosition = (List<String>) object;
        }
    }

    /**
     * 获取测量位置
     *
     * @return 测量位置集合
     */
    public List<String> getMeasurePosition() {
        return mMeasurePosition;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
